#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>


int main()
{
    printf("I am a father process, pid: %d, ppid: %d\n", getpid(), getppid());

    pid_t id = fork();
    if(id == 0)
    {
        // child
        int cnt = 5;
        while(cnt)
        {
            printf("I am a child process, pid: %d, ppid: %d, cnt: %d\n", getpid(), getppid(), cnt);
            sleep(1);
        }
        printf("child quit...\n");
        exit(123);
    }
    sleep(7); // 形成僵尸状态
    // father
    //pid_t rid = wait(NULL);
    int status = 0;
    pid_t rid = waitpid(id, &status, 0);
    if(rid > 0) printf("wait success! rid: %d\n", rid);
    sleep(3);
    printf("father quit..., status: %d, child quit code: %d, child quit signal: %d\n", status, (status >> 8)&0xFF, status&0x7F );
}


//int main()
//{
//    for(int i = 0; i < 255; i++)
//        printf("%d -> %s\n", i, strerror(i));
//
//   // printf("I am a parent process, pid: %d, ppid: %d\n", getpid(), getppid());
//   // sleep(2);
//
//    return 100;
//}
//
